
#include "FrameData.h"
#include "Utils.h"
using namespace std;




FrameData *  FrameData::CreateWithJson(const string &  json)
{
	FrameData*ret = new FrameData(json);
	if (ret)return ret;
	return  nullptr;
}

FrameData *  FrameData::CreateEmpty()
{
	FrameData*ret = new FrameData();
	if (ret)return ret;
	return  nullptr;
}


FrameData::FrameData(const string& json)
{
	this->InitWithJson(json);
}

FrameData::FrameData()
{
	dir = -1;
	op = -1;
	fps = -1;
	no = 0;
}

string  FrameData::toJson()
{
	string ret;
	ret.append("no:");
	ret += Utils::itos(no);
	ret.append(",");

	ret.append("dir:");
	ret += Utils::itos(dir);
	ret.append(",");

	ret.append("op:");
	ret += Utils::itos(op);
	ret.append(",");

	ret.append("fps:");
	ret += Utils::itos(fps);
	ret.append(",");

	return ret;
}


void  FrameData::InitWithJson(const string & json)
{
	int last = 0;
	string k, v;
	//parse kv to map
	for (int i = 0; i < json.size(); i++)
	{
		if (json[i] == ':')
		{
			k = json.substr(last, i - last);
			last = i + 1;
		}

		if (json[i] == ',')
		{
			v = json.substr(last, i - last);
			last = i + 1;
			kv[k] = v;
		}
	}
	//parse map to member data
	this->Parse();
}

void  FrameData::Parse()
{
	//parse no
	string & s_no = kv["no"];
	if (s_no != "")
	{
		this->no = std::stoi(s_no);
	}
	else
	{
		this->no = -1;
	}


	//parse dir
	string & s_dir = kv["dir"];
	if (s_dir != "")
	{
		this->dir = std::stoi(s_dir);
	}
	else
	{
		this->dir = -1;
	}


	//parse op
	string &s_op = kv["op"];
	if (s_op != "")
	{
		this->op = std::stoi(s_op);
	}
	else
	{
		this->op = -1;
	}
	//parse fps
	string &s_fps = kv["fps"];
	if (s_fps != "")
	{
		this->fps = std::stoi(s_fps);
	}
	else
	{
		this->fps = -1;
	}

}


FrameData::~FrameData()
{

}